package gui.feeds;

import gui.GUIUtils;
import gui.utilities.DateDiffCalculator;

import java.awt.Dimension;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import core.exceptions.DBAccessException;
import core.objects.Country;
import core.objects.NewsFeedItem;
import core.objects.User;
import core.objects.enums.GenderEnum;
import db.DBAccessEngine;
import db.threads.DBAccessThread;
import db.threads.IDBAccessThread;

public class JFeedType8 extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    public static final int OPEN_PANEL_HEIGHT = 400;
    public static final int CLOSE_PANEL_HEIGHT = 100;

	private NewsFeedItem item;
		
	public JFeedType8(NewsFeedItem item) {
		this.item = item;
        initComponents();
        setMaximumHeight(CLOSE_PANEL_HEIGHT);
	}
	
    private void setMaximumHeight(int height) {
        setMaximumSize(new Dimension(Integer.MAX_VALUE, height));
        setPreferredSize(new Dimension(getWidth(), height));
    }

    private void initComponents() {
    	
    	setBorder(new EmptyBorder(10, 10, 10, 10));
    	
    	jPanel = new JPanel();
        jLabelImage = new javax.swing.JLabel();
        jLabelUserName = new javax.swing.JLabel();
        jLabelCountry = new javax.swing.JLabel();
        jLabelEmail = new javax.swing.JLabel();
        jLabelCheckOut = new javax.swing.JLabel();
        
        setBackground(new java.awt.Color(51, 153, 255));
        setAlignmentX(0.0F);
        setAlignmentY(0.0F);
        setOpaque(false);
        
        jLabelImage.setAlignmentY(0.0F);
        jLabelImage.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 51, 51), 1, true));
        jLabelImage.setMaximumSize(new java.awt.Dimension(20, 9));
        jLabelImage.setPreferredSize(new java.awt.Dimension(2, 90));
        
        jPanel.setBackground(new java.awt.Color(51, 153, 255));
        jPanel.setBorder(new EmptyBorder(0, 8, 0, 8));
        jPanel.setSize(getWidth(), CLOSE_PANEL_HEIGHT);

        {
	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
	        this.setLayout(layout);
	
	        GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup();
	        leftToRight.addComponent(jLabelImage);
	        leftToRight.addComponent(jPanel);
	        
	        GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup();
	        GroupLayout.ParallelGroup row = layout.createParallelGroup();
	        row.addComponent(jLabelImage);
	        row.addComponent(jPanel);
	        topToBottom.addGroup(row);

	        leftToRight.addGap(15, 15, 15);
	        topToBottom.addGap(15, 15, 15);

	        layout.setHorizontalGroup(leftToRight);
	        layout.setVerticalGroup(topToBottom);
        }
        
        {
	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel);
	        jPanel.setLayout(layout);
	
	        GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup();
	        GroupLayout.ParallelGroup column = layout.createParallelGroup();
	        column.addComponent(jLabelUserName);
	        column.addComponent(jLabelEmail);
	        column.addComponent(jLabelCountry);
	        column.addComponent(jLabelCheckOut);
	        leftToRight.addGroup(column);
	        
	        GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup();
	        topToBottom.addComponent(jLabelUserName);
	        topToBottom.addComponent(jLabelEmail);
	        topToBottom.addComponent(jLabelCountry);
	        topToBottom.addComponent(jLabelCheckOut);

	        leftToRight.addGap(15, 15, 15);
	        topToBottom.addGap(15, 15, 15);
	        
	        layout.setHorizontalGroup(leftToRight);
	        layout.setVerticalGroup(topToBottom);
        }
        
        DBAccessThread.invoke(new IDBAccessThread() {
			
			@Override
			public Object onCreateQuery() throws DBAccessException {
				return DBAccessEngine.User().select(item.getUsername());
			}
			
			@SuppressWarnings("unchecked")
			@Override
			public void onAccessFinished(Object response) {
				List<User> users = (List<User>)response;
				if (users.size() > 0) {
					GUIUtils.setProfileImageAtLabel(users.get(0), jLabelImage);	
					jLabelEmail.setText("Email: " + users.get(0).getEmail());
					if (users.get(0).getGender() == GenderEnum.Female) {
				        jLabelCheckOut.setText("<html><i>Be sure to check out her book collection!</i></html>");
					}
					
					//this is already running in a separate-from-GUI thread:
					if (users.get(0).getCountryID() > 0) {
						try {
							List<Country> countries = (List<Country>)DBAccessEngine.Country().select(users.get(0).getCountryID());
							if (countries.size() > 0) {
								jLabelCountry.setText("From: " + countries.get(0).getName());
							}
						} catch (DBAccessException exception) {
							//do nothing, keep label as it is
						}
					}
					
				} else {
					//empty user for non gender empty picture:
					GUIUtils.setProfileImageAtLabel(new User(0, ""), jLabelImage);
				}
			}
			
			@Override
			public void onAccessFailed(DBAccessException e) {
				//empty user for non gender empty picture:
				GUIUtils.setProfileImageAtLabel(new User(0, ""), jLabelImage);
			}
		});

        GUIUtils.setJComponentFontAndColor(jLabelUserName, 15);
        GUIUtils.setJComponentFontAndColorNonBold(jLabelEmail, 15);
        GUIUtils.setJComponentFontAndColorNonBold(jLabelCountry, 15);
        GUIUtils.setJComponentFontAndColor(jLabelCheckOut, 15);        
        
        //placehold with empty picture:
        GUIUtils.setProfileImageAtLabel(new User(0, ""), jLabelImage);
        
        jLabelUserName.setText("<html><u>" + item.getUsername()
        		+ "</u> has joined <i>Loan-A-Book</i> <font style=font-size:9px>("
        		+ DateDiffCalculator.getDiffString(item.getStartDate())
        		+ ")</font></html>");
        jLabelEmail.setText("Email: ");
        jLabelCountry.setText("From: ");
        jLabelCheckOut.setText("<html><i>Be sure to check out his book collection!</i></html>");
        
    }
    
    private javax.swing.JLabel jLabelImage;
    private javax.swing.JPanel jPanel;
    private javax.swing.JLabel jLabelUserName;
    private javax.swing.JLabel jLabelCountry;
    private javax.swing.JLabel jLabelEmail;
    private javax.swing.JLabel jLabelCheckOut;

}
